﻿#include <graphics.h>
#include <iostream>
#include <easyx.h>
#include <conio.h>
#include "unit.h"
#include <map>
#include <string>
#include <vector>
#include<thread>
#include<deque>
struct Footprint {
    int x, y;
    int startTime;       // 烟圈生成时间
    float radius;        // 当前半径
    float maxRadius;     // 最大膨胀半径
    bool isActive;       // 是否处于活跃状态
};

std::deque<Footprint> footprints;

void drawFootprints() {
    int currentTime = clock();

    for (auto it = footprints.begin(); it != footprints.end();) {
        Footprint& fp = *it;
        float elapsed = currentTime - fp.startTime;

        // 生命周期计算（持续2秒）
        if (elapsed > 2000) { // 2000ms = 2秒
            it = footprints.erase(it);
            continue;
        }

        // 半径增长计算（0.5秒内达到最大半径）
        float radiusProgress = elapsed / 500.0f; // 500ms内完成膨胀
        if (radiusProgress > 1.0f) radiusProgress = 1.0f;
        fp.radius = 3 + (fp.maxRadius - 3) * radiusProgress;

        // 透明度计算（从黑到白渐变）
        float alpha = elapsed / 2000.0f; // 2秒内从0到1
        if (alpha > 1.0f) alpha = 1.0f;
        int grayValue = static_cast<int>(255 * alpha);

        // 设置颜色（RGB从0,0,0渐变到255,255,255）
        setfillcolor(RGB(grayValue, grayValue, grayValue));
        solidcircle(fp.x + 100, fp.y + 200, static_cast<int>(fp.radius));

        // 添加随机漂移效果
        fp.x += (rand() % 101) - 50;  // -2到+2的随机水平位移
        fp.y -= 1 + (rand() % 20);  // 持续向上漂移

        ++it;
    }
}







enum Size
{
    WinH = 512, WinW = 1024, d = 10, SmokeSum = 10
};

void putTranPhoto(int x, int y, IMAGE* mask, const IMAGE* img)
{
    putimage(x, y, mask, SRCAND);
    putimage(x, y, img, SRCPAINT);
}
//光栅三位运算函数（x，y，白底，黑底）

void setfullscreen()
{
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    HWND hwnd = GetHWnd();
    LONG style = GetWindowLong(hwnd, GWL_STYLE);
    style &= ~WS_OVERLAPPEDWINDOW;
    style |= WS_POPUP;
    SetWindowLong(hwnd, GWL_STYLE, style);
    SetWindowPos(hwnd, HWND_TOP, 0, 0, screenWidth, screenHeight, SWP_NOZORDER | SWP_FRAMECHANGED);
}
//设置全屏函数。


#include <graphics.h>
#include <conio.h>

#include <graphics.h>
#include <conio.h>

int lastSpawnTime = 0;    // 上次生成烟圈的时间

void loadImages(std::map<std::string, IMAGE*>& zt, const std::vector<std::string>& prefixes, const std::vector<std::string>& suffixes, int width, int height)
{
    for (const auto& prefix : prefixes)
    {
        for (const auto& suffix : suffixes)
        {
            std::string key = prefix + suffix;
            zt[key] = new IMAGE;
            std::string path = "zhuangtai/" + key + ".jpg";
            loadimage(zt[key], path.c_str(), width, height);
        }
    }
}
//加载图片

Unit* mainUnit;

void somkeNow()
{
    IMAGE* smokew = new IMAGE;
    IMAGE* smokeb = new IMAGE;

    for (int i = 0; i < Size::SmokeSum; ++i)
    {

        loadimage(smokew, "zhuangtai/somkew.jpg", 304, 224);
        loadimage(smokeb, "zhuangtai/smokeb.jpg", 304, 224);
        putTranPhoto(mainUnit->get_x(), mainUnit->get_y(), smokew, smokeb);

        Sleep(30);
    }
}

int main()
{
    srand(static_cast<unsigned>(time(nullptr)));

    initgraph(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
    setfullscreen();
    setorigin(0, 0);
    setbkcolor(RED);
    int x = GetSystemMetrics(SM_CXSCREEN) / 2 - 80, y = GetSystemMetrics(SM_CYSCREEN) / 2 - 80;
    cleardevice();

    mainUnit = new Unit(x, y, 25, 98);
    IMAGE* bkphoto = new IMAGE;
    loadimage(bkphoto, "bkphoto.jpg");
    std::map<std::string, IMAGE*> zt;
    std::vector<std::string> prefixes =
    { "z000", "z001", "z010", "z011", "z100", "z101", "z110", "z111",
      "y000", "y001", "y010", "y011", "y100", "y101", "y110", "y111",
        "up","down"
    };
    std::vector<std::string> suffixes = { "w", "b" };
    loadImages(zt, prefixes, suffixes, 464*2, 184*2);

    //std::thread t(somkeNow);

    for (int i = 0; i <= 10; ++i)
    {
        std::string key = "hp" + std::to_string(i);
        zt[key] = new IMAGE;
        std::string path = "zhuangtai/" + key + ".jpg";
        loadimage(zt[key], path.c_str());
    }

    int X = 0;
    int Y = 0;
    std::string str = "z000";
    std::string s = "up";
    while (true)
    {
        BeginBatchDraw();
        bool moved = false;

        if (GetAsyncKeyState(VK_UP) & 0x8000 || GetAsyncKeyState('W') & 0x8000)
        {
            mainUnit->changeData(mainUnit->get_x(), mainUnit->get_y() - Size::d, mainUnit->get_size());
            Y += Size::d;
            moved = true;
            s = "up";
        }
        else if (GetAsyncKeyState(VK_DOWN) & 0x8000 || GetAsyncKeyState('S') & 0x8000)
        {
            mainUnit->changeData(mainUnit->get_x(), mainUnit->get_y() + Size::d, mainUnit->get_size());
            Y -= Size::d;
            moved = true;
            s = "down";
        }
        else if (GetAsyncKeyState(VK_LEFT) & 0x8000 || GetAsyncKeyState('A') & 0x8000)
        {
            str[0] = 'z';
            mainUnit->changeData(mainUnit->get_x() - Size::d, mainUnit->get_y(), mainUnit->get_size());
            X += Size::d;
            moved = true;
            s.clear();
        }
        else if (GetAsyncKeyState(VK_RIGHT) & 0x8000 || GetAsyncKeyState('D') & 0x8000)
        {
            str[0] = 'y';
            mainUnit->changeData(mainUnit->get_x() + Size::d, mainUnit->get_y(), mainUnit->get_size());
            X -= Size::d;
            moved = true;
            s.clear();
        }
        else if (GetAsyncKeyState('Y') & 0x8000 && str[1] < '1')
        {
            str[1] += 1;
            moved = true;
        }
        else if (GetAsyncKeyState('H') & 0x8000 && str[1] > '0')
        {
            str[1] -= 1;
            moved = true;
        }
        else if (GetAsyncKeyState('U') & 0x8000 && str[2] < '1')
        {
            str[2] += 1;
            moved = true;
        }
        else if (GetAsyncKeyState('J') & 0x8000 && str[2] > '0')
        {
            str[2] -= 1;
            moved = true;
        }
        else if (GetAsyncKeyState('I') & 0x8000 && str[3] < '1')
        {
            str[3] += 1;
            moved = true;
        }
        else if (GetAsyncKeyState('K') & 0x8000 && str[3] > '0')
        {
            str[3] -= 1;
            moved = true;
        }
        
        cleardevice();
        setorigin(X, Y);
        putimage(0, 0, bkphoto);
        if (clock() - lastSpawnTime > 50)
        {
            footprints.push_back
            (
                {
                mainUnit->get_x() + (rand() % 7 - 3),  // 添加随机水平偏移
                mainUnit->get_y() + (rand() % 5 - 2),  // 添加随机垂直偏移
                clock(),          // 记录生成时间
                3.0f * 10,             // 初始半径
                (8.0f + (rand() % 5))*10,// 随机最大半径（8-12）
                true              // 活跃状态
                }
            );
            lastSpawnTime = clock();
        }
        drawFootprints();
        setfillcolor(RED);
        putimage(mainUnit->get_x(), mainUnit->get_y() - 30, zt["hp" + std::to_string(mainUnit->showHP())]);
        if(s.empty())
            putTranPhoto(mainUnit->get_x()-350, mainUnit->get_y()-50, zt[str + "w"], zt[str + "b"]);
        else
        {
            putTranPhoto(mainUnit->get_x()-350, mainUnit->get_y()-50, zt[s + "w"], zt[s + "b"]);
        }
        EndBatchDraw();
        moved ? Sleep(30): Sleep(100);
    }
    //按键操作。


    delete mainUnit;
    delete bkphoto;
    for (auto& pair : zt)
    {
        delete pair.second;
    }
    closegraph();
    //空间释放以及关闭画布。
    return 0;
}